<template>
    <div v-loading="loading" element-loading-text="拼命加载中">

        <div class="content_top" style="height: 30px;">
            <el-button icon="el-icon-plus" type="success" circle size="mini" @click="showSave(null)" style="float: right"></el-button>
        </div>

        <el-table :data="data.data" border stripe highlight-current-row style="width: 100%" size="mini">
            <el-table-column type="index" header-align="center" align="center" width="60px"/>
            <el-table-column prop="phone" label="手机号" header-align="center" align="center"/>
            <el-table-column prop="nickName" label="姓名" header-align="center" align="center"/>

            <el-table-column prop="status" label="状态" header-align="center" align="center" width="70px;">
                <template slot-scope="scope">
                    {{ scope.row.status === 1 ? "启用" : "停用" }}
                </template>
            </el-table-column>
            <el-table-column prop="createTime" label="创建时间" header-align="center" align="center"/>
            <el-table-column prop="action" fixed="right" label="操作" header-align="center" align="center">
                <template slot-scope="scope">
                    <el-button type="text" size="mini" @click="showSave(scope.row)">编辑</el-button>
                </template>
            </el-table-column>
        </el-table>

        <div class="content_bottom" style="text-align: center">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                           :current-page="condition.currentPage" :page-size="condition.pageSize" :total="data.total"
                           :page-sizes="[10, 15, 20, 30, 50, 80, 100]" layout="total, sizes, prev, pager, next, jumper">
            </el-pagination>
        </div>

        <el-dialog title="保存" :visible.sync="dialogVisible" width="500px" append-to-body>
            <div>
                <el-form ref="form" :model="form" :rules="rules" label-width="95px" size="mini">
                    <el-form-item label="手机号" prop="phone">
                        <el-input type="number" v-model="form.phone" style="width: 305px;" :readonly="selectRow !== null"></el-input>
                    </el-form-item>
                    <el-form-item label="姓名" prop="nickName">
                        <el-input v-model="form.nickName" style="width: 305px;"></el-input>
                    </el-form-item>

                    <el-form-item label="状态" prop="status">
                        <el-radio-group v-model="form.status">
                            <el-radio :label="1">启用</el-radio>
                            <el-radio :label="2">停用</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-form>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="onSubmit" :loading="btnLoading">保存</el-button>
            </div>
        </el-dialog>
    </div>

</template>

<script>
    import {userList, add, modify} from '@/api/verify/user'

    export default {
        name: 'VerifyUser',
        data() {
            return {
                loading: false,
                btnLoading: false,
                dialogVisible: false,
                condition: {
                    currentPage: 1,
                    pageSize: 30
                },
                data: {},
                selectRow: null,
                form: {
                    phone: null,
                    nickName: null,
                    status: 1
                },
                rules: {
                    phone: [
                        {required: true, message: '请输入手机号', trigger: 'blur'},
                        {min: 11, max: 11, message: '长度为11个字符', trigger: 'blur'}
                    ],
                    nickName: [
                        {required: true, message: '请输入姓名', trigger: 'blur'},
                        {min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur'}
                    ],
                    status: [
                        {required: true, message: '请输入商品名称', trigger: 'blur'},
                    ]
                }
            }
        },
        computed: {},
        mounted() {
            this.loadList()
        },
        methods: {
            handleSizeChange(val) {
                this.condition.pageSize = val
                this.loadList()
            },
            handleCurrentChange(val) {
                this.condition.currentPage = val
                this.loadList()
            },
            async loadList() {
                this.loading = true
                let res = await userList(this.condition)
                this.loading = false
                if (res.code !== "0") return
                this.data = res
            },
            async showSave(row) {
                this.dialogVisible = true
                this.selectRow = row
                if (row) {
                    if (this.$refs.form) {
                        this.$refs.form.resetFields();
                    }
                    this.form.phone = row.phone
                    this.form.nickName = row.nickName
                    this.form.status = row.status
                } else {
                    if (this.$refs.form) {
                        this.$refs.form.resetFields();
                    }
                }
            },
            onSubmit() {
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        this.save()
                    } else {
                        return false;
                    }
                });
            },
            async save () {
                this.btnLoading = true
                let res = null
                if (this.selectRow) {
                    res = await modify(this.selectRow.id, this.form)
                } else {
                    res = await add(this.form)
                }
                this.btnLoading = false
                if (res.code !== '0') {
                    this.$message.warning(res.message)
                    return
                }
                this.$message({
                    type: 'success',
                    message: '保存成功!'
                });
                this.dialogVisible = false
                this.loadList()
            }
        }
    }
</script>

<style scoped>
</style>
